﻿
#region Usings
using System;
using System.Collections.Generic;

#endregion

namespace LongNet.Util.DataTypes
{
    /// <summary>
    /// Used to count the number of times something is added to the list
    /// </summary>
    public class Bag<T> : ICollection<T>
    {
        #region Constructors

        /// <summary>
        /// Constructor
        /// </summary>
        public Bag()
        {
        }

        #endregion

        #region Private Variables

        protected Dictionary<T, int> Items = new Dictionary<T, int>();

        #endregion

        #region ICollection<T> Members

        public void Add(T item)
        {
            if (Items.ContainsKey(item))
                ++Items[item];
            else
                Items.Add(item, 1);
        }

        public void Clear()
        {
            Items.Clear();
        }

        public bool Contains(T item)
        {
            return Items.ContainsKey(item);
        }

        /// <summary>
        /// Not implemented
        /// </summary>
        /// <param name="array">Not used</param>
        /// <param name="arrayIndex">Not used</param>
        public void CopyTo(T[] array, int arrayIndex)
        {
            throw new NotImplementedException();
        }

        public int Count
        {
            get { return Items.Count; }
        }

        public bool IsReadOnly
        {
            get { return false; }
        }

        public bool Remove(T item)
        {
            return Items.Remove(item);
        }

        #endregion

        #region IEnumerable<T> Members

        public IEnumerator<T> GetEnumerator()
        {
            foreach (T Key in this.Items.Keys)
            {
                yield return Key;
            }
        }

        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            foreach (T Key in this.Items.Keys)
            {
                yield return Key;
            }
        }

        #endregion

        #region Properties

        public int this[T index]
        {
            get { return Items[index]; }
            set { Items[index] = value; }
        }

        #endregion
    }
}